package com.tdtxdcxm.ruralcommunity.slice;

import com.tdtxdcxm.ruralcommunity.ResourceTable;
import com.tdtxdcxm.ruralcommunity.appruntime.DataCenterControl;
import com.tdtxdcxm.ruralcommunity.myutils.MyTools;
import com.tdtxdcxm.ruralcommunity.providers.LCProviderForNormal;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class ContentDiscussSlice extends AbilitySlice {
    private String contentid;
    private Button discuss_backbut,discuss_gobut;
    private TextField discuss_writetfd;
    private ListContainer discuss_lc;

    /*<----------------------------------------初始化ListContainer区域----------------------------------*/
    public void initListContainer(ListContainer listContainer,String listContainerName,AbilitySlice slice){
        if(listContainer == null){return;}
        listContainer.setName(listContainerName);
        LCProviderForNormal lcProviderForNormal = new LCProviderForNormal(DataCenterControl.discuss_listcontainer_list,this);
        DataCenterControl.discussListContainerProvider = lcProviderForNormal;
        listContainer.setItemProvider(lcProviderForNormal);
    }
    /*-----------------------------------------初始化ListContainer区域---------------------------------->*/

    private void initCDSComponents(){
        discuss_backbut = (Button) findComponentById(ResourceTable.Id_discuss_backbut);
        discuss_gobut = (Button) findComponentById(ResourceTable.Id_discuss_gobut);
        discuss_writetfd = (TextField) findComponentById(ResourceTable.Id_discuss_writetfd);
        discuss_lc = (ListContainer) findComponentById(ResourceTable.Id_discuss_lc);

        discuss_backbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DataCenterControl.discuss_listcontainer_list.clear();
                discuss_writetfd.clearFocus();
                discuss_writetfd.setText("");
                terminate();
            }
        });
        discuss_gobut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(discuss_writetfd.getText().equals("")){
                    MyTools.toastShow("评论内容不能为空！",ContentDiscussSlice.this.getContext());
                    return;
                }
                MyTools.UpDataContentDiscussInfo(ContentDiscussSlice.this,contentid,discuss_writetfd.getText(),discuss_writetfd);
            }
        });
        discuss_writetfd.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if(s.length() > 299){
                    discuss_writetfd.setText(s.substring(0,s.length() - 1));
                    MyTools.toastShow("不能超过300字！",ContentDiscussSlice.this.getContext());
                }
            }
        });

        initListContainer(discuss_lc,"discusslc",this);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_usercontent_discuss);
        initCDSComponents();
        contentid = intent.getStringParam("contentid");
        MyTools.DownDataContentDiscussListInfo(this, contentid);
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
